package com.xjh.controller;

import com.xjh.bo.AclUserBO;
import com.xjh.utils.resultutils.ResponseObj;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@RequestMapping("/common")
public class LoginController {

    @PostMapping("/login")
    public ResponseObj login(AclUserBO userBO, HttpServletRequest httpServletRequest) {


        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok();
    }



    // 下面都是获取登录用户的一些信息
    @GetMapping("/getLoginUserByPrincipal")
    public String getLoginUserByPrincipal(Principal principal) {
        return principal.getName();
    }

    @GetMapping(value = "/getLoginUserByAuthentication")
    public String currentUserName(Authentication authentication) {
        return authentication.getName();
    }

    @GetMapping(value = "/username")
    public String currentUserNameSimple(HttpServletRequest request) {

        Principal principal = request.getUserPrincipal();
        return principal.getName();
    }

    @GetMapping("/getLoginUser")
    public String getLoginUser() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user.getUsername();
    }
}